package com.putao.album.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.provider.Settings;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.putao.album.application.GlobalApplication;

import java.io.File;
import java.text.DecimalFormat;

/**
 * Created by jidongdong on 15/4/23.
 */
public class AppHelper {

    static Context context;

    static {
        context = GlobalApplication.getInstance();
    }

    //获取版本号(内部识别号)
    public static int getVersionCode() {
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }


    /**
     * 获取当前应用的版本号
     * 例如:1.1.0
     *
     * @return String
     */
    public static String getVersionName() {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            return info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 获取设备Id
     * A 64-bit number (as a hex string) that is randomly generated
     * when the user first sets up the device and should remain constant
     * for the lifetime of the user's device.
     *
     * @return
     */
    public static String getDeviceId() {
//        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//        String deviceId = tm.getDeviceId();
        String android_id = Settings.Secure.getString(GlobalApplication.getInstance().getContentResolver(),
                Settings.Secure.ANDROID_ID);
        return android_id;
    }

    /**
     *获取存储卡上应用数据的路径,参见PuTaoConstants.APP_FOLDER
     * @return
     */
    public static File getAppDiskDir() {
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory(),PuTaoConstants.APP_FOLDER);
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }
        return mediaStorageDir;
    }

    /**
     * 获取图片缓存的路径
     * @return
     */
    public static File getAlbumDiskCacheDir() {
        File mediaStorageDir = new File(getAppDiskDir(),"cache");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }
        return mediaStorageDir;
    }

    /**
     * 获取缓存图片占用的空间
     */
    public static String getAlbumDiskCacheSize() {
        File cacheDir = ImageLoader.getInstance().getDiskCache().getDirectory();
        long length=0;
        File[] files =cacheDir.listFiles();
        for(int i=0;i<files.length;i++){
            length+=files[i].length();
        }

        if (length > 1024 * 1024) {
            return (new DecimalFormat(".00").format((float) length / (1024 * 1024))) + "M";
        } else if (length > 1024) {
            return (new DecimalFormat(".00").format((float) length / 1024)) + "KB";
        } else {
            return length + "B";
        }
    }

    /**
     * 清理所有图片缓存
     */
    public static void clearAlbumDiskCache() {
        ImageLoader.getInstance().clearDiskCache();
    }
}
